﻿using System;
using System.Collections.Generic;

using System.Text;

namespace NRainel.Log
{
    public class Loger
    {
        #region 字段
        
        List<IRecordLog> _recordLogList = new List<IRecordLog>();
        string _location;
        #endregion

        #region 构造

        public Loger()
        { }

        public Loger(Type type)
        {
            _location = type.FullName;
        }

        public Loger(string location)
        {
            _location = location;
        }
        
        #endregion

        /// <summary>
        /// 日志记录器集合
        /// </summary>
        public List<IRecordLog> Records
        {
            get { return _recordLogList; }
        }

        /// <summary>
        /// 是否记录操作日志
        /// </summary>
        public bool RecordInfo{ get; set; }
        
        public void Error(string message,Exception exc)
        {
            Error(message, exc, null);
        }

        public void Error(string message,Exception exc, params object[] args)
        {
            if (args != null && args.Length > 0)
            {
                message = string.Format(message, args);
            }
            string msg = string.Format("[Msg]:{0}\r\n[Exception]:{1}", message,exc.ToString());
            RecordMes(msg, LogTypeEnum.Error);
        }

        public void Info(string message)
        {
            Info(message, null);
        }

        public void Info(string message, params object[] args)
        {
            if (RecordInfo)
            {
                if (args != null && args.Length > 0)
                {
                    message = string.Format(message, args);
                }
                string msg = string.Format("[Msg]:{0}", message);
                RecordMes(msg, LogTypeEnum.Info);
            }
        }

        private void RecordMes(string info,LogTypeEnum logType)
        {
            string log = string.Format("/*------------------------------------START------------------------------------\r\n[DateTime]:{0}\r\n[Location:]{1}\r\n{2}\r\n*/\r\n "
                ,DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss,fff")
                ,_location
                ,info);

            foreach (var rocord in Records)
            {
                rocord.RecordLog(log, logType);
            }
            
        }
    }
}
